package jju.game;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class StartMenu extends Frame implements KeyListener{
    private Hero play1, play2;
    private Score score;
    private Map mp;
    private Image offScreenImage = null;

    public static void main(String[] args) {
        StartMenu startMenu = new StartMenu();
    }

    /**
     * 双缓冲技术
     */

    @Override
    public void update(Graphics g) {
        paint(g);
        score.paint(g);
    }

    StartMenu(){
        setBounds(0,0,1500,800);
        play1 = new Hero(200,500, 1);
        play2 = new Hero(1400,500, 2);
        PaintThread.start();
        score = new Score();

        //计分线程
        ScoreThread scoreThread = new ScoreThread(play1,play2, score);
        scoreThread.start();

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        addKeyListener(this);
        setResizable(false);
        setVisible(true);
    }

    public void paint(Graphics g){
        mp = new Map();
        mp.paint(g);
        play1.paint(g);
        play2.paint(g);
    }

//    @Override
//    public void paintComponents(Graphics g) {
//        paint(g);
//    }

    Thread PaintThread = new Thread(){
        @Override
        public void run() {
            while (true){
                try {
                    repaint();
                    Thread.sleep(15);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    };

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        play1.pressAction(e.getKeyCode());
        play2.pressAction(e.getKeyCode());
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }
}